<?php
/*
Bitsand - a web-based booking system for LRP events
Copyright (C) 2006 Russell Peter Phillips

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

include ('../inc/inc_head_db.php');
include ('../inc/inc_admin.php');
include ('../inc/inc_head_html.php');

$db_prefix = DB_PREFIX;

if ($_POST ['btnSubmit'] != '' && CheckReferrer ('admin/admin_markpaid.php'))
foreach ($_POST as $key => $value) {
	if (substr ($key, 0, 7) == "hPlayer") {
		$iPlayerID = $value;
		$paid = $_POST ["chkPayPl{$value}"];
		$meal = $_POST ["chkMealPl{$value}"];
		$gate = $_POST ["chkGatePl{$value}"];

		//Mark player as paid
		if ($paid != 0) {
			//Set up UPDATE & SELECT queries
			$sql_update = "UPDATE {$db_prefix}bookings SET bkDatePaymentConfirmed = '" . date ('Y-m-d') . "' WHERE bkPlayerID = " . $iPlayerID;
			$sql_select = "SELECT plFirstName, plSurname, plEmail ";
			$sql_select .= "FROM {$db_prefix}players WHERE plPlayerID = " . $iPlayerID;
			//Run UPDATE query to set paid date
			ba_db_query ($link, $sql_update);
			//Run SELECT query and send e-mail
			$result = ba_db_query ($link, $sql_select);
			$row = ba_db_fetch_assoc ($result);
			$sBody = "Your payment for the upcoming event has been received and you have been marked as paid.\n" .
			$sBody .= "You are now fully booked.\n\nThank you.\n\n";
			$sBody .= "Player ID: " . PID_PREFIX . sprintf ('%03s', $iPlayerID) . "\n";
			$sBody .= "OOC Name: " . $row ['plFirstName'] . " " . $row ['plSurname'];
			mail ($row ['plEmail'], SYSTEM_NAME . ' - marked paid', $sBody, "From:" . SYSTEM_NAME . " <" . EVENT_CONTACT_MAIL . ">");
		}
		//Update meal ticket field
		if ($meal == '')
			$meal = 0;
		//Set up UPDATE query
		$sql = "UPDATE {$db_prefix}bookings SET bkMealTicket = $meal WHERE bkPlayerID = " . $iPlayerID;
		//Run UPDATE query to set meal ticket
		ba_db_query ($link, $sql);
		//Update pay on gate field
		if ($gate == 1)
			$sql = "UPDATE {$db_prefix}bookings SET bkDatePaymentConfirmed = '" . date ('Y-m-d') . "', bkPayOnGate = 1 " .
				"WHERE bkPlayerID = " . $iPlayerID;
		else
			$sql = "UPDATE {$db_prefix}bookings SET bkPayOnGate = 0 WHERE bkPlayerID = " . $iPlayerID;
		//Run UPDATE query to set paid date
		ba_db_query ($link, $sql);
	}
}

//Get list of players booked but not marked as paid
$sql = "SELECT plPlayerID, " . 
	"plFirstName, " .
	"plSurname, " .
	"plBookAs, " .
	"chName, " .
	"bkDateOOCConfirmed, " .
	"bkDateICConfirmed, " .
	"bkDatePaymentConfirmed " .
	"FROM {$db_prefix}players, {$db_prefix}characters, {$db_prefix}bookings " .
	"WHERE plPlayerID = chPlayerID AND chPlayerID = bkPlayerID AND " .
	"bkDateOOCConfirmed <> '0000-00-00' AND bkDateICConfirmed <> '0000-00-00' AND bkDatePaymentConfirmed = '0000-00-00' ORDER BY plPlayerID";
$result = ba_db_query ($link, $sql);
?>
<script src="../inc/sorttable.js" type="text/javascript"></script>

<h1><?php echo TITLE?> - Payments Received</h1>

<p>
<a href = 'admin.php'>Admin</a>
</p>

<p>
The following people have booked, but are not marked as paid. Click on a column header to sort by that column.
</p>

<form action = 'admin_markpaid.php' method = 'post'>

<table border = '1' class="sortable">
<tr>
<th>Paid?</th>
<th>Meal Ticket?</th>
<th>Pay on Gate</th>
<th>Player ID</th>
<th>OOC First Name</th>
<th>OOC Surname</th>
<th>IC Name</th>
<th>Booking As</th>
</tr>

<?php
while ($row = ba_db_fetch_assoc ($result)) {
	echo "<tr class = 'highlight'>";
	echo "<td class = 'mid'><input type = 'hidden' name = 'hPlayer" . $row ['plPlayerID'] . "' value = '" . $row ['plPlayerID'] . "'>";
	echo "<input type = 'checkbox' name = 'chkPayPl" . $row ['plPlayerID'] . "' value = '" . $row ['plPlayerID'] . "'></td>";
	echo "<td class = 'mid'><input type = 'checkbox' name = 'chkMealPl" . $row ['plPlayerID'] . "' value = '" . $row ['plPlayerID'] . "'></td>";
	echo "<td class = 'mid'><input type = 'checkbox' name = 'chkGatePl" . $row ['plPlayerID'] . "' value = '" . $row ['plPlayerID'] . "'></td>";
	echo "<td>" . PID_PREFIX . sprintf ('%03s', $row ['plPlayerID']) . "</td>";
	echo "<td>" . htmlentities (stripslashes ($row ['plFirstName'])) . "</td>";
	echo "<td>" . htmlentities (stripslashes ($row ['plSurname'])) . "</td>";
	echo "<td>" . htmlentities (stripslashes ($row ['chName'])) . "</td>";
	echo "<td>" . htmlentities (stripslashes ($row ['plBookAs'])) . "</td>";
}
?>

</table>

<p>
<input type = 'submit' value = 'Submit' name = 'btnSubmit'>&nbsp;
<input type = 'reset' value = 'Reset'>
</p>
</form>

<?php
include ('../inc/inc_foot.php');
?>
